/***************************************************************************
/
/ the InfoRequestController class manages all the user actions related
/ to viewing, creating and submitting a request for information
/
/**************************************************************************/

class InfoRequestController {


	//define the default redirection for the InfoRequestController
    def index = { redirect( action : create, params : params ) }


    // the delete, save and update actions only accept POST requests
    def allowedMethods = [ delete : 'POST', save : 'POST', update : 'POST' ]


	//show a specific InfoRequest record
    def show = {

		//get the approriate record based on the passed in id parameter
        def infoRequest = InfoRequest.get( params.id )

		//check we have a valid record
        if( !infoRequest ) {

			//if the record is not found, return to the list page with en error message
            flash.message = "InfoRequest not found"
            redirect( action : list )

        } else {

			//redirect to the 'view' page for this record
			return [ infoRequest : infoRequest ]

		}
    }


	//create a brand new InfoRequest record, which will be populated by the user
    def create = {

        def infoRequest = new InfoRequest()
        infoRequest.properties = params
        return [ 'infoRequest' : infoRequest ]

    }


	//save the user request to the database
    def save = {

        def infoRequest = new InfoRequest( params )
        infoRequest.save()

		// check for errors
		if ( !infoRequest.hasErrors() ) {

			//if no errors redirect to the submitted page and show a thank you message
            flash.message = "Thank You! Your request for information has been sent, you will be contacted by Seattle MicroFund shortly."
            redirect( action : show, id : infoRequest.id )

		} else {

			//if errors return to view page and show error message
            render( view : 'create', model : [ infoRequest : infoRequest ] )

        }
    }

}